package com.wondersgroup.core.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import com.github.pagehelper.PageHelper;
import com.wondersgroup.core.mapper.AppointmentMapper;
import com.wondersgroup.core.model.Appointment;
import com.wondersgroup.core.resultmap.DTOAppointment;

@Service
public class AppointmentService {
    

    @Autowired
    private AppointmentMapper appointmentMapper;
    
    /**
     * 根据实体类不为null的字段进行查询,条件全部使用=号and条件
     * @param appointment
     * @return
     * @author sunny wang
     */
    public List<Appointment> select(Appointment appointment){
        return appointmentMapper.select(appointment);
    }
    
    /**
     * 根据实体类不为null的字段进行查询,条件全部使用=号and条件
     * @param appointment
     * @return
     * @author sunny wang
     */
    public List<Appointment> select(Appointment appointment, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return appointmentMapper.select(appointment);
    }
    
    /**
     * //根据实体类不为null的字段查询总数,条件全部使用=号and条件
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int selectCount(Appointment appointment){
        return appointmentMapper.selectCount(appointment);
    }

    /**
     * 根据主键进行查询,必须保证结果唯一 . 
     * 单个字段做主键时,可以直接写主键的值 
     * 联合主键时,key可以是实体类,也可以是Map
     * @param primarykey
     * @return
     * @author sunny wang
     */
    public Appointment selectByPrimaryKey(Integer primarykey){
        return appointmentMapper.selectByPrimaryKey(primarykey);
    }

    /**
     * 插入一条数据
     * 支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
     * 优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int insert(Appointment appointment){
        int row = appointmentMapper.insert(appointment);
        return row;
    }

    /**
     * 插入一条数据,只插入不为null的字段,不会影响有默认值的字段.
     * 支持Oracle序列,UUID,类似Mysql的INDENTITY自动增长(自动回写)
     * 优先使用传入的参数值,参数值空时,才会使用序列、UUID,自动增长
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int insertSelective(Appointment appointment){
        return appointmentMapper.insertSelective(appointment);
    }

    /**
     * 根据实体类中字段不为null的条件进行删除,条件全部使用=号and条件
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int delete(Appointment appointment){
        return appointmentMapper.delete(appointment);
    }

    /**
     * //通过主键进行删除,这里最多只会删除一条数据
     * 单个字段做主键时,可以直接写主键的值
     * 联合主键时,key可以是实体类,也可以是Map
     * @param primarykey
     * @return
     * @author sunny wang
     */
    public int deleteByPrimaryKey(Integer primarykey){
        return appointmentMapper.deleteByPrimaryKey(primarykey);
    }

    /**
     * //根据主键进行更新,这里最多只会更新一条数据
     * 参数为实体类
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int updateByPrimaryKey(Appointment appointment){
        return appointmentMapper.updateByPrimaryKey(appointment);
    }
    
    /**
     * 根据主键进行更新
     * 只会更新不是null的数据
     * @param appointment
     * @return
     * @author sunny wang
     */
    public int updateByPrimaryKeySelective(Appointment appointment){
        return appointmentMapper.updateByPrimaryKeySelective(appointment);
    }
    
    public String getAppointmentMaxFlowNo(){
    	String flowno = appointmentMapper.getAppointmentMaxFlowNo();
		Date date = new Date();         
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    	String firstno = formatter.format(date) + "00001";
    	if(StringUtils.isEmpty(flowno) || firstno.compareTo(flowno) > 0){
    		flowno = firstno;
    	}else{
			DecimalFormat df = new DecimalFormat("00000");
    		flowno = formatter.format(date) 
					+ df.format(1 + Integer.parseInt(flowno.substring(8, 13)));
    	}
    	return flowno;
    }
    
    /**
     * (APP端)获取预约列表
     * @param pageSize2 
     */
	public List<DTOAppointment> queryAppointmentList(Integer userId,
			Integer customerId, Integer companyId, Integer hospitalId,
			Integer status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Integer> paraMap = new HashMap<>();
        paraMap.put("userId", userId);
        paraMap.put("customerId", customerId);
        paraMap.put("companyId", companyId);
        paraMap.put("hospitalId", hospitalId);
        paraMap.put("status", status);
		return appointmentMapper.queryAppointmentList(paraMap);
	}
	/**
     * 根据客户ID查找预约列表
     * @return 
     */
    public List<DTOAppointment> queryAppointmentListByConditions(String customerIds, int pageNum, int pageSize) {
    	PageHelper.startPage(pageNum, pageSize);
    	 Map<String, String> paraMap = new HashMap<>();
    	 paraMap.put("customerIds", customerIds);
		return appointmentMapper.queryAppointmentListByConditions(paraMap);
    }

    /**
     * (保险公司端)获取预约列表
     * @param pageSize2 
     */
	public List<Appointment> queryCompanyAppointmentList(Integer companyId,
			Integer customerId,String customerName, String hospitalName, 
			String startDate, String endDate,Integer status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("companyId", companyId);
        paraMap.put("customerId", customerId);
        paraMap.put("customerName", customerName);
        paraMap.put("hospitalName", hospitalName);
        paraMap.put("startDate", startDate);
        paraMap.put("endDate", endDate);
        paraMap.put("status", status);
		return appointmentMapper.queryCompanyAppointmentList(paraMap);
	}
	
	/**
     * (保险公司端)查询首页待受理预约申请数
     */
	public int queryHomeAppointmentCount(Integer companyId, Integer status) {
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("companyId", companyId);
        paraMap.put("status", status);
        List<Appointment> list = appointmentMapper.queryCompanyAppointmentList(paraMap);
		return list==null?0:list.size();
	}
}
